/**
 * 工单管理组合式函数
 * 
 * 这个可组合函数提供了工单管理页面所需的核心功能，包括：
 * - 工单列表数据获取和分页管理
 * - 工单统计信息展示
 * - 工单查询条件管理
 * - 用户视角的工单过滤（支持普通用户和管理员不同视图）
 * 
 * @module useWorkOrder
 */
import { ref, reactive } from 'vue'
import * as WorkOrder from '../../api/WorkOrder'
import { ElMessage } from 'element-plus'

// 定义表单数据类型
interface WorkOrderFormData {
  name: string | null
  createUser: string | null
  woCategory: number | null
  woStatus: number | null
  woPriority: number | null
  dateRange: string[] | null
  userName: string | null
  woUserName: string | null
}

// 定义统计卡片数据类型
interface StatsCardData {
  title: string
  type: string
  value: number
  key: string
}

export function useWorkOrder(options?: { userId?: number }) {
  const currentPage = ref(1)
  const pageSize = ref(10)
  const dataTotal = ref(0)
  const tableData = ref<any[]>([])
  const statsCardsData = ref<StatsCardData[]>([
    { title: '工单总数', key: 'total', type: 'primary', value: 0 },
    { title: '已激活', key: 'activated', type: 'danger', value: 0 },
    { title: '已解决', key: 'solved', type: 'success', value: 0 },
    { title: '已关闭', key: 'closed', type: 'info', value: 0 }
  ])

  const formData = reactive<WorkOrderFormData>({
    name: null,
    createUser: null,
    woCategory: null,
    woStatus: null,
    woPriority: null,
    dateRange: null,
    userName: null,
    woUserName: null
  })

  // 获取工单列表数据
  const getDataInfo = async () => {
    const params: any = {
      pageNum: currentPage.value,
      pageSize: pageSize.value
    }
    // 如果是用户视角，添加用户过滤条件
    if (options?.userId) {
      params.userId = options.userId
    }
    // 如果有用户名称，添加用户名称过滤条件
    if (formData.userName !== null) params.userName = formData.userName
    if (formData.woUserName !== null) params.woUserName = formData.woUserName
    if (formData.woStatus !== null) params.woStatus = formData.woStatus
    if (formData.woPriority !== null) params.woPriority = formData.woPriority
    if (formData.woCategory !== null) params.woCategory = formData.woCategory

    try {
      const { rows, total } = await WorkOrder.getList(params)
      tableData.value = rows
      dataTotal.value = total

      await getNumber()
    } catch (error) {
      ElMessage.error('获取数据失败')
      console.error(error)
    }
  }

  // 获取统计数据
  const getNumber = async () => {
    const params: any = {
      pageNum: currentPage.value,
      pageSize: pageSize.value
    }

    // 如果是用户视角，添加用户过滤条件
    // if (options?.userId) {
    //   params.userId = options.userId
    // }
    if (formData.userName !== null) params.userName = formData.userName
    if (formData.woUserName !== null) params.woUserName = formData.woUserName
    // if (formData.woStatus !== null) params.woStatus = formData.woStatus
    // if (formData.woPriority !== null) params.woPriority = formData.woPriority
    // if (formData.woCategory !== null) params.woCategory = formData.woCategory

    const data0 = await WorkOrder.getList(params)
    statsCardsData.value[0].value = data0.total

    // 已激活
    params.woStatus = 1
    const data1 = await WorkOrder.getList(params)
    statsCardsData.value[1].value = data1.total

    // 已解决
    params.woStatus = 2
    const data2 = await WorkOrder.getList(params)
    statsCardsData.value[2].value = data2.total

    // 已关闭
    params.woStatus = 0
    const data3 = await WorkOrder.getList(params)
    statsCardsData.value[3].value = data3.total
  }

  // 分页相关方法
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    currentPage.value = 1
    getDataInfo()
  }

  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    getDataInfo()
  }

  // 查询和重置
  const handleClickBtnQuery = () => {
    currentPage.value = 1
    getDataInfo()
  }

  const handleClickBtnReset = () => {
    formData.name = null
    formData.createUser = null
    formData.woCategory = null
    formData.woStatus = null
    formData.woPriority = null
    formData.dateRange = null
    getDataInfo()
  }

  /**
   * 点击卡片
   * @param item 
   */
  const handleClickCardItem = (item: any) => {
    switch (item.key) {
      case 'total':
        formData.woCategory = null
        formData.woStatus = null
        formData.woPriority = null
        break
      case 'activated':
        formData.woCategory = null
        formData.woStatus = 1
        formData.woPriority = null
        break
      case 'solved':
        formData.woCategory = null
        formData.woStatus = 2
        formData.woPriority = null
        break
      case 'closed':
        // 已关闭
        formData.woCategory = null
        formData.woStatus = 0
        formData.woPriority = null
        break
    }
    getDataInfo()
  }
  return {
    // 响应式数据
    currentPage,
    pageSize,
    dataTotal,
    tableData,
    statsCardsData,
    formData,

    // 方法
    getDataInfo,
    getNumber,
    handleSizeChange,
    handleCurrentChange,
    handleClickBtnQuery,
    handleClickBtnReset,
    handleClickCardItem,
  }
}